home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_unicode_file.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  6KB  |  191 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import os
  5. import glob
  6. import time
  7. import shutil
  8. import unicodedata
  9. import unittest
  10. from test.test_support import run_suite, TestSkipped, TESTFN_UNICODE
  11. from test.test_support import TESTFN_ENCODING, TESTFN_UNICODE_UNENCODEABLE
  12.  
  13. try:
  14.     TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
  15. except (UnicodeError, TypeError):
  16.     raise TestSkipped('No Unicode filesystem semantics on this platform.')
  17.  
  18. if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
  19.     import sys
  20.     
  21.     try:
  22.         TESTFN_UNICODE = unicode('@test-\xe0\xf2', sys.getfilesystemencoding())
  23.         TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
  24.         if '?' in TESTFN_ENCODED:
  25.             raise UnicodeError, 'mbcs encoding problem'
  26.     except (UnicodeError, TypeError):
  27.         raise TestSkipped('Cannot find a suiteable filename.')
  28.     except:
  29.         None<EXCEPTION MATCH>(UnicodeError, TypeError)
  30.     
  31.  
  32. None<EXCEPTION MATCH>(UnicodeError, TypeError)
  33. if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
  34.     raise TestSkipped('Cannot find a suitable filename.')
  35.  
  36.  
  37. def remove_if_exists(filename):
  38.     if os.path.exists(filename):
  39.         os.unlink(filename)
  40.     
  41.  
  42.  
  43. class TestUnicodeFiles(unittest.TestCase):
  44.     
  45.     def _do_single(self, filename):
  46.         self.failUnless(os.path.exists(filename))
  47.         self.failUnless(os.path.isfile(filename))
  48.         self.failUnless(os.access(filename, os.R_OK))
  49.         self.failUnless(os.path.exists(os.path.abspath(filename)))
  50.         self.failUnless(os.path.isfile(os.path.abspath(filename)))
  51.         self.failUnless(os.access(os.path.abspath(filename), os.R_OK))
  52.         os.chmod(filename, 511)
  53.         os.utime(filename, None)
  54.         os.utime(filename, (time.time(), time.time()))
  55.         self._do_copyish(filename, filename)
  56.         self.failUnless(os.path.abspath(filename) == os.path.abspath(glob.glob(filename)[0]))
  57.         (path, base) = os.path.split(os.path.abspath(filename))
  58.         if isinstance(base, str):
  59.             base = base.decode(TESTFN_ENCODING)
  60.         
  61.         file_list = os.listdir(path)
  62.         base = unicodedata.normalize('NFD', base)
  63.         file_list = [ unicodedata.normalize('NFD', f) for f in file_list ]
  64.         self.failUnless(base in file_list)
  65.  
  66.     
  67.     def _do_equivilent(self, filename1, filename2):
  68.         self.failUnless(type(filename1) != type(filename2), 'No point checking equivalent filenames of the same type')
  69.         self.failUnlessEqual(os.stat(filename1), os.stat(filename2))
  70.         self.failUnlessEqual(os.lstat(filename1), os.lstat(filename2))
  71.         self._do_copyish(filename1, filename2)
  72.  
  73.     
  74.     def _do_copyish(self, filename1, filename2):
  75.         self.failUnless(os.path.isfile(filename1))
  76.         os.rename(filename1, filename2 + '.new')
  77.         self.failUnless(os.path.isfile(filename1 + '.new'))
  78.         os.rename(filename1 + '.new', filename2)
  79.         self.failUnless(os.path.isfile(filename2))
  80.         
  81.         try:
  82.             filename1 == filename2
  83.         except UnicodeDecodeError:
  84.             pass
  85.  
  86.         shutil.copy(filename1, filename2 + '.new')
  87.         os.unlink(filename1 + '.new')
  88.         shutil.move(filename1, filename2 + '.new')
  89.         self.failUnless(not os.path.exists(filename2))
  90.         shutil.move(filename1 + '.new', filename2)
  91.         self.failUnless(os.path.exists(filename1))
  92.         shutil.copy2(filename1, filename2 + '.new')
  93.         os.unlink(filename1 + '.new')
  94.  
  95.     
  96.     def _do_directory(self, make_name, chdir_name, encoded):
  97.         cwd = os.getcwd()
  98.         if os.path.isdir(make_name):
  99.             os.rmdir(make_name)
  100.         
  101.         os.mkdir(make_name)
  102.         
  103.         try:
  104.             os.chdir(chdir_name)
  105.             
  106.             try:
  107.                 if not encoded:
  108.                     cwd_result = os.getcwdu()
  109.                     name_result = make_name
  110.                 else:
  111.                     cwd_result = os.getcwd().decode(TESTFN_ENCODING)
  112.                     name_result = make_name.decode(TESTFN_ENCODING)
  113.                 cwd_result = unicodedata.normalize('NFD', cwd_result)
  114.                 name_result = unicodedata.normalize('NFD', name_result)
  115.                 self.failUnlessEqual(os.path.basename(cwd_result), name_result)
  116.             finally:
  117.                 os.chdir(cwd)
  118.  
  119.         finally:
  120.             os.rmdir(make_name)
  121.  
  122.  
  123.     
  124.     def _test_single(self, filename):
  125.         remove_if_exists(filename)
  126.         f = file(filename, 'w')
  127.         f.close()
  128.         
  129.         try:
  130.             self._do_single(filename)
  131.         finally:
  132.             os.unlink(filename)
  133.  
  134.         self.failUnless(not os.path.exists(filename))
  135.         f = os.open(filename, os.O_CREAT)
  136.         os.close(f)
  137.         
  138.         try:
  139.             self._do_single(filename)
  140.         finally:
  141.             os.unlink(filename)
  142.  
  143.  
  144.     
  145.     def _test_equivalent(self, filename1, filename2):
  146.         remove_if_exists(filename1)
  147.         self.failUnless(not os.path.exists(filename2))
  148.         f = file(filename1, 'w')
  149.         f.close()
  150.         
  151.         try:
  152.             self._do_equivilent(filename1, filename2)
  153.         finally:
  154.             os.unlink(filename1)
  155.  
  156.  
  157.     
  158.     def test_single_files(self):
  159.         self._test_single(TESTFN_ENCODED)
  160.         self._test_single(TESTFN_UNICODE)
  161.         if TESTFN_UNICODE_UNENCODEABLE is not None:
  162.             self._test_single(TESTFN_UNICODE_UNENCODEABLE)
  163.         
  164.  
  165.     
  166.     def test_equivalent_files(self):
  167.         self._test_equivalent(TESTFN_ENCODED, TESTFN_UNICODE)
  168.         self._test_equivalent(TESTFN_UNICODE, TESTFN_ENCODED)
  169.  
  170.     
  171.     def test_directories(self):
  172.         ext = '.dir'
  173.         self._do_directory(TESTFN_ENCODED + ext, TESTFN_ENCODED + ext, True)
  174.         self._do_directory(TESTFN_ENCODED + ext, TESTFN_UNICODE + ext, True)
  175.         self._do_directory(TESTFN_UNICODE + ext, TESTFN_ENCODED + ext, False)
  176.         self._do_directory(TESTFN_UNICODE + ext, TESTFN_UNICODE + ext, False)
  177.         if TESTFN_UNICODE_UNENCODEABLE is not None:
  178.             self._do_directory(TESTFN_UNICODE_UNENCODEABLE + ext, TESTFN_UNICODE_UNENCODEABLE + ext, False)
  179.         
  180.  
  181.  
  182.  
  183. def test_main():
  184.     suite = unittest.TestSuite()
  185.     suite.addTest(unittest.makeSuite(TestUnicodeFiles))
  186.     run_suite(suite)
  187.  
  188. if __name__ == '__main__':
  189.     test_main()
  190.  
  191.